
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        :permission="permissionList"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page">
        <template slot="menu" slot-scope="scope">
          <el-button
            v-if="permissionList.editBtn && scope.row.handleStatus == 1"
            type="text" size="small"
            icon="el-icon-edit"
            @click="handleEdit(scope.row, scope.$index)">
            编辑
          </el-button>
        </template>
        <template slot="menuLeft">
          <el-button type="primary" size="small" @click="confirmBatch">一键确认</el-button>
          <el-button type="primary" size="small" @click="handleExport">导 出</el-button>
          <el-button type="primary" size="small" :hidden="true" v-if="false" @click="handlePrint">打 印</el-button>
        </template>
      </avue-crud>
     <el-dialog :visible.sync="editShow" title="调入站完善信息">
        <avue-form ref="form" v-model="editForm" :option="editOption">
          <template slot-scope="scope" slot="menuForm">
            <el-button type="primary" v-if="false" @click="feedback">异常反馈</el-button>
            <el-button type="primary" @click="submit">保存</el-button>
            <el-button @click="cancle">取消</el-button>
          </template>
        </avue-form>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {
  putObj,
  confirm,exportExcel
} from "@/api/subwaystm/swstcyclein";
import { handleExcelResponse } from "@/util/util";

export default {
  name: "swstcyclein",
  mixins: [baseCrud('subwaystm/swstcyclein')],
  data() {
    return {
      params: {
        inOrOut: 0
      },
      editShow: false,
      editForm: {},
      editOption: {
        submitBtn: false,
        emptyBtn: false,
        group: [{
          label: '调入信息',
          column: [{
            "type": "select",
            "label": "调入线路",
            "prop": "inLineCode",
            detail: true,
            disabled: true,
            cascaderItem: ["inStationCode"],
            props: {
              label: 'name',
              value: 'code'
            },
            dicUrl: "/support/swparamline/dict"
          }, {
            "type": "select",
            "label": "调入站点",
            "prop": "inStationCode",
            dicFlag: false,
            disabled: true,
            props: {
              label: 'name',
              value: 'code'
            },
            dicUrl: "/support/swparamstation/dict/{{key}}"
          }, {
            "type": "datetime",
            "label": "调入时间",
            "prop": "inTime",
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            rules:[{
              required:true,
              message:'请输入调入时间',
              trigger:"blur"
            }],
          },
          // {
          //   "type": "input",
          //   "label": "票种",
          //   "prop": "cardType",
          //   dicUrl: '/support/swpmticketcardtype/dict',
          //   disabled: true
          // }, {
          //   "type": "input",
          //   "label": "车票名称",
          //   "prop": "cardTypeParam",
          //   dicUrl: '/support/swpmticketcardtypeparam/dict',
          //   disabled: true
          // },
          {
            label: '票种',
            prop: 'cardType',
            search: true,
            type: 'select',
            dicUrl: '/support/swpmticketcardtype/dict',
            props: {
              label: 'mainName',
              value: 'mainEnName'
            },
            disabled: true
          },	  {
            label: '车票名称',
            prop: 'cardTypeParam',
            type: 'select',
            props: {
              label: 'subName',
              value: 'subEnName'
            },
            dicUrl: '/support/swpmticketcardtypeparam/dictAll/{{key}}',
            disabled: true
          },
          {
            "type": "number",
            "label": "金额",
            "prop": "money",
	     precision: 2,
            placeholder: ' ',
            disabled: true
          }, {
            "type": "input",
            "label": "次数",
            "prop": "amount",
            placeholder: ' ',
            disabled: true
          }, {
            "type": "input",
            "label": "特殊工作卡卡号",
            "prop": "specialCardNo",
             disabled: true
          }, {
            "type": "select",
            "label": "接收人",
            "prop": "inPerson",
            component: 'user-select',
            params: {
              userCodeKey: 'inPersonCode',
              belongToStation: true
            },
            rules:[{
              required:true,
              message:'请输入接收人',
              trigger:'blur'
            }]
          }, {
            "type": "input",
            "label": "接收人工号",
            "prop": "inPersonCode",
            placeholder: ' ',
            disabled: true
          },
          {
            "type": "input",
            "label": "调出数量",
            "prop": "outToNumber",
            display: false
          },
          {
            "type": "input",
            "label": "接收数量",
            "prop": "inNumber",
            rules:[{
              required:true,
              message:'请输入接收数量',
              trigger:'blur'
            }]
          }, {
            "type": "input",
            "label": "差异数量",
            "prop": "diffNumber",
            disabled: true
          }, {
            "type": "textarea",
            "label": "反馈内容",
            "prop": "backMessage",
            span: 24
          }]
        }]
      }
    };
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.stm_swstcycle_add, false),
        delBtn: this.vaildData(this.permissions.stm_swstcycle_del, false),
        editBtn: this.vaildData(this.permissions.stm_swstcycle_edit, false)
      };
    }
  },
  watch: {
    'editForm.inNumber': {
      handler(newValue, oldValue){
          this.editForm.diffNumber = Number(this.editForm.outToNumber)-Number(this.editForm.inNumber);
      },
      immediate: true
    }
  },
  methods: {
    async handleExport(){
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        let tableSelect = this.$refs.crud.tableSelect;
        let ids = tableSelect.map(row => row.id)
        let param = {
          inOrOut: '0',
          ids: this.validatenull(ids)?null:ids
        };
        const res = await exportExcel(param);
        handleExcelResponse(res);
      } finally {
        loading.close();
      }
    },
    confirmBatch(){
      var tableSelect = this.$refs.crud.tableSelect
      if(tableSelect.length == 0){
        this.$message.info("请选择要确认的数据！");
        return;
      }
      for (const table of this.$refs.crud.tableSelect) {
        if (this.validatenull(table.outToNumber) || table.outToNumber<=0) {
          this.$message.error("请检查所选数据，需要调出车站先填写调出数量等信息！");
          return false;
        }
      }
      confirm(tableSelect).then(resonse => {
        if (resonse.data.code == "0") {
          this.$message.success("确认成功");
          this.getList(this.page);
        }else{
          this.$message.error("确认失败");
        }
      }).catch(error => {
        this.$message.error("确认失败");
      })
    },
    handleEdit(row, index){
      this.editForm = row
      this.editShow = true
    },
    feedback(){

    },
    submit(){
      this.$refs.form.validate(vaild=>{
        if(vaild){
          this.editForm.isIn = '1'
          putObj(this.editForm).then(data => {
            this.$message.success("修改成功");
            this.editShow = false;
            this.getList(this.page);
          })
        }
      })
    },
    cancle(){
      this.editShow = false;
    }
  }
};
</script>
